<script setup>
import { ref, onMounted } from 'vue'
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
// 需要导入模块（如果子模块有导出的方法的话）
const channelList = ref([])
const loading = ref(false)
const dialog = ref()
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  loading.value = false
  console.log(channelList.value)
}
onMounted(() => {
  getChannelList() // 一进页面就加载数据
})
const onEditChannel = (row, $index) => {
  // 编辑事件
  console.log(row, $index)
  dialog.value.open(row)
}
const onDelChannel = async (row) => {
  // 删除事件
  // 提示框  ElMessageBox.confirm（‘内容’，‘标题’，{配置项}）
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage.success('删除成功')
  // ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}
const onAddChannel = () => {
  //添加分类
  dialog.value.open({})
}
// 实时更新列表值
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <!-- 传给子普通参数，动态参数是 :titel -->
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>

    <el-table :data="channelList" v-loading="loading" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>

      <el-table-column label="操作" width="100">
        <!-- prop是对象（数据）的属性 -->
        <!-- row 就是 channelList 的一项，类似于item； $index 下标 -->
        <template #default="{ row }">
          <!--default是默认插槽，可以在多个默认插槽内做区分-->
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 弹窗 -->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
    <!-- 子给老爹的事件名@success,要监听,并给对应的事件函数 -->
  </page-container>
</template>
